from django.db import models
from datetime import datetime

from user.models import User


# Create your models here.
class MatchItem(models.Model):
    class Meta:
        db_table = "match_item"
        managed = True
        verbose_name_plural = "比赛"

    STATUS = (
        ('0','未开始'),
        ('1','已经始'),
        ('2','已经束'),
    )

    id = models.AutoField(primary_key=True)
    name = models.CharField("比赛名称",max_length=30,null=False,default=None)
    status = models.CharField("状态",choices = STATUS,null=False,max_length=10,default=None)

    # contestant = models.ForeignKey('MatchContestant',blank=True,on_delete=models.CASCADE,)

    def __str__(self):
        return self.name

    # def matchName(self):
    #     return self.match_name

class MatchContestant(models.Model):
    class Meta:
        db_table = "match_contestant"
        verbose_name_plural = "人员名单"
        managed = True

    id = models.AutoField(primary_key=True)
    area = models.CharField("地区",max_length=50,null=True,default=None)
    studuent_num = models.CharField("学号",max_length=50,null=False)
    name = models.CharField("学生姓名",max_length=15,null=False)
    academy_name = models.CharField("学院名称",max_length=50,null=True,default=None)
    mobile = models.CharField("手机",max_length=11,null=False)
    note = models.CharField("备注",max_length=100,blank=True,default=None)
    gmt_create = models.DateTimeField("创建时间",default=datetime.now)
    gmt_modified = models.DateTimeField("修改时间",default=datetime.now)
    match_item = models.ManyToManyField('MatchItem',blank=True)
    user_account = models.OneToOneField(User,unique=True,null=False,on_delete=models.CASCADE,)

    def __str__(self):
        return self.name


class MatchScore(models.Model):
    class Meta:
        db_table = "match_score"
        verbose_name_plural = "成绩表"
        managed = True

    id = models.AutoField(primary_key=True)

    match_item = models.ForeignKey('MatchItem',verbose_name='参赛项目',null=False,default=None,on_delete=models.CASCADE,)
    contestant = models.ForeignKey('MatchContestant',verbose_name='参赛选手名',null=False,default=None,on_delete=models.CASCADE,)
    score = models.CharField('成绩',max_length=3,default=0,blank=True)
    hand_in_time = models.DateTimeField('交卷时间',default=datetime.now)

    # match_item = models.ForeignKey('MatchItem',blank=True)
    def __str__(self):
        return str(self.match_item)
